package com.stt.videoprocess.design.aspect;

import com.stt.videoprocess.design.model.VideoTask;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class PerformanceMetricsAspect {
    private static final Logger logger = LoggerFactory.getLogger(PerformanceMetricsAspect.class);

    @Around("execution(* com.stt.videoprocess.design.processor..*.process(..))")
    public Object logProcessingTime(ProceedingJoinPoint joinPoint) throws Throwable {
        VideoTask task = (VideoTask) joinPoint.getArgs()[0];
        String processorName = joinPoint.getTarget().getClass().getSimpleName();
        
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long duration = System.currentTimeMillis() - startTime;

        String metric = String.format("%s: %dms", processorName, duration);
        task.setProcessingSteps(task.getProcessingSteps() + metric + "\n");
        logger.info("Processor {} executed in {}ms", processorName, duration);
        
        return result;
    }
}